﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Read.Api.Application.Command;
using B.S.BaseData.Read.Api.Dtos;
using MediatR;
using SqlSugar;

namespace B.S.BaseData.Read.Api.Application.CommandHander
{
    public class GetDepmentHander : IRequestHandler<GetDepmentCommand, ApiResult<List<DepmentDto>>>
    {

        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public GetDepmentHander(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 部门显示
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<List<DepmentDto>>> Handle(GetDepmentCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<DepmentDto>> result = new ApiResult<List<DepmentDto>>();

            var entity = db.Queryable<Depment>();

            result.Code = ApiEnum.显示成功;
            result.Msg = ApiEnum.显示成功.ToString();
            result.Data = mapper.Map<List<DepmentDto>>(entity.ToList());

            return Task.FromResult(result);
        }
    }
}
